package com.gitee.ly;

import lombok.Data;
import org.noear.snack.core.utils.StringUtil;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.util.Assert;

import java.net.InetAddress;
import java.net.UnknownHostException;

//除了必须配置的字段，其它字段可以有默认值
@Data
public class LightRegistryProperties {
    private boolean enable = true; //true代表使用动态模式  false代表使用固定url模式(需要NamiClient配置url)
    private String serverBaseUrl; // 必须配置该字段
    private String serviceName;
    private String schema = "http";
    private String ip;
    private int port;
    private String headerU;  //必须配置该字段

    public LightRegistryProperties(AppContext context) throws UnknownHostException {
        this.enable = context.cfg().getBool("light.registry.enable", true);
        this.serverBaseUrl = context.cfg().get("light.registry.serverBaseUrl", "");
        this.headerU = context.cfg().get("light.registry.headerU", "");
        this.schema = context.cfg().get("light.registry.schema", "http");
        this.serviceName = context.cfg().get("light.registry.serviceName", "");
        this.ip = context.cfg().get("light.registry.ip", "");
        this.port = context.cfg().getInt("light.registry.port", 0);
        //ServiceName默认值
        if (StringUtil.isEmpty(this.serviceName)) {
            this.serviceName = context.cfg().getProperty("solon.app.name");
            Assert.notEmpty(this.serviceName, "solon.app.name can't be empty");
        }
        //ip默认值
        if (StringUtil.isEmpty(this.ip)) {
            this.ip = InetAddress.getLocalHost().getHostAddress();
        }
        //port默认值
        if (this.port <= 1) {
            this.port = Integer.parseInt(context.cfg().getProperty("server.port"));
        }
    }
}
